package com.gitee.huanminabc.db_freedom.base;

import com.gitee.huanminabc.jcommon.reflect.FieldUtil;
import com.gitee.huanminabc.jcommon.str.StringUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ResultSet 工具类
 *
 * @Author: huanmin
 * @Date: 2025/4/5 11:16
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
public class ResultSetUtil {
    //将结果集转为实体类
    public static <T> T getObject(ResultSet rs, Class<T> tClass) throws SQLException {
        T t = null;
        try {
            t = tClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        try {
            while (rs.next()) {
                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    String columnName = rs.getMetaData().getColumnName(i);
                    //判断是否有下划线 ,遇到_则转为驼峰 ,如 user_name -> userName
                    if (columnName.contains("_")) {
                        columnName = StringUtil.toCamelCase(columnName);
                    }
                    Object value = rs.getObject(i);
                    FieldUtil.setFieldValue(t, columnName, value);
                }
            }

        } finally {
            rs.close();
        }
        return t;
    }

    //将多行结果集转为实体类
    public static <T> List<T> getObjectList(ResultSet rs, Class<T> tClass) throws SQLException {
        List<T> list = new ArrayList<>();
        try {
            while (rs.next()) {
                T t = null;
                try {
                    t = tClass.newInstance();
                } catch (InstantiationException | IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    String columnName = rs.getMetaData().getColumnName(i);
                    //判断是否有下划线 ,遇到_则转为驼峰 ,如 user_name -> userName
                    if (columnName.contains("_")) {
                        columnName = StringUtil.toCamelCase(columnName);
                    }
                    Object value = rs.getObject(i);
                    FieldUtil.setFieldValue(t, columnName, value);
                }
                list.add(t);
            }
        } finally {
            rs.close();
        }
        return list;
    }

    //将结果集转为Map
    public static Map<String, Object> getMap(ResultSet rs) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        try {
            while (rs.next()) {
                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    map.put(rs.getMetaData().getColumnName(i), rs.getObject(i));
                }
            }
        } finally {
            rs.close();
        }

        return map;
    }

    //将多行结果集转为List<Map>
    public static List<Map<String, Object>> getListMap(ResultSet rs) throws SQLException {
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            while (rs.next()) {
                Map<String, Object> map = new HashMap<>();
                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    map.put(rs.getMetaData().getColumnName(i), rs.getObject(i));
                }
                list.add(map);
            }
        } finally {
            rs.close();
        }
        return list;
    }




}
